# -*- coding: utf-8 -*-
import scrapy,json,re,time
from scrapy import Request,Spider
from weibo.weibo.items import UserItem,UserRelationItem,WeiboItem

class WeibocnSpider(scrapy.Spider):
    name = 'weibocn'
    allowed_domains = ['m.weibo.com']
    user_url='https://m.weibo.cn/api/container/getIndex?uid={uid}&type=uid&value={uid}&containerid=100505{uid}'
    follow_url='https://m.weibo.cn/api/container/getIndex?containerid=231051_-_followers_-_{uid}&page={page}'
    fan_url='https://m.weibo.cn/api/container/getIndex?containerid=231051_-_fans_-_{uid}&since_id={page}'
    weibo_url='https://m.weibo.cn/api/container/getIndex?containerid=230413{uid}_-_WEIBO_SECOND_PROFILE_WEIBO&page={page}'
    start_users = ['1569160390','1651428902']


    def start_requests(self):
        for uid in self.start_users:
            yield Request(self.user_url.format(uid=uid),callback=self.parse_user)
    def parse_user(self, response):
        # self.logger.dubug(response)
        result=json.loads(response.text)
        if result.get('data').get('userInfo'):
            user_info=result.get('data').get('userInfo')
            user_item=UserItem()
            field_map={
                'id':'id','name':'screen_name','fans_count':'followers_count',
                'follows_count':'follow_count','weibos_count':'statuses_count'
            }
            for field,attr in field_map.items():
                user_item[field]=user_info.get[attr]
            yield user_item
            uid=user_info.get('id')
            yield Request(self.follow_url.format(uid=uid,page=1),callback=self.parse_follows,
                          meta={'page':'1','uid':'uid'})
            yield Request(self.fan_url.format(uid=uid,page=1),callback=self.parse_fans,
                          meta={'page':'1','uid':'uid'})
            yield Request(self.weibo_url.format(uid=uid,page=1),callback=self.parse_weibos,
                          meta={'page':'1','uid':'uid'})

    def parse_follows(self,response):
        result=json.loads(response.text)
        if result.get('ok') and result.get('data').get('cards'):
            follows=result.get('data').get('cards')[-1].get('card_group')
            for follow in follows:
                if follow.get('user'):
                    uid=follow.get('user').get('id')
                    yield Request(self.user_url.format(uid=uid),callback=self.parse_user)
            uid=response.meta.get('uid')
            user_relation_item=UserRelationItem()
            follows=[{'id':follow.get('user').get('id'),'name':follow.get('user').get('user').get('screen_name')}
                     for follow in follows ]
            user_relation_item['id']=uid
            user_relation_item['follows']=follows
            user_relation_item['fans']=[]
            yield user_relation_item
            page=response.meta.get('page') +1
            yield Request(self.follow_url.format(uid=uid,page=page),
                          callback=self.parse_follows,meta={'page':page,'uid':uid})

    def parse_weibos(self,response):
        result=json.loads(response.text)
        if result.get('ok') and result.get('data').get('cards'):
            weibos=result.get('data').get('cards')
            for weibo in weibos:
                mblog=weibo.get('mblog')
                if mblog:
                    weibo_item=WeiboItem()
                    field_map={
                        'id':'id','text':'text'
                    }
                    for field,attr in field_map.items():
                        weibo_item[field]=mblog.get(attr)
            weibo_item['user']=response.meta.get('uid')
            yield weibo_item
        uid=response.meta.get('uid')
        page=response.meta.get('page')+1
        yield Request(self.weibo_url.format(uid=uid,page=page)
                      ,callback=self.parse_weibos,meta={'uid':uid,'page':page})


